<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\api\controller;


use app\api\logic\UserLogic;
use app\api\validate\PasswordValidate;
use app\api\validate\SetUserInfoValidate;
use app\api\validate\UserValidate;
use app\common\model\article\Enterprise;
use app\common\model\article\Procure;
use app\common\model\article\Supply;
use app\common\model\qualifications\Qualifications;
use app\common\model\user\User;
use think\facade\Db;
/**
 * 用户控制器
 * Class UserController
 * @package app\api\controller
 */
class UserController extends BaseApiController
{
    public array $notNeedLogin = ['resetPassword'];


    /**
     * @notes 获取个人中心
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 段誉
     * @date 2022/9/16 18:19
     */
    public function center()
    {
        $data = UserLogic::center($this->userInfo);
        return $this->success('', $data);
    }


    /**
     * @notes 获取个人信息
     * @return \think\response\Json
     * @author 段誉
     * @date 2022/9/20 19:46
     */
    public function info()
    {
        $result = UserLogic::info($this->userId);
        return $this->data($result);
    }

    public function gongyin()
    {
        $user=$this->userId;
        $model=new Supply();
        $name=$this->request->get("name");
        if (!empty($name)) {
            $model = $model->where('name', 'like', '%' . $name . '%');
        }
        $result=   $model->where("user_id","=",$user)->select()->toArray();
        return $this->success("",$result,1,1);
    }

    public function caigou()
    {
        $user=$this->userId;
        $model=new Procure();
        $name=$this->request->get("name");
        if (!empty($name)) {
            $model = $model->where('name', 'like', '%' . $name . '%');
        }
        $result=   $model->where("user_id","=",$user)->select()->toArray();
        return $this->success("",$result,1,1);
    }
    /**
     * @notes 重置密码
     * @return \think\response\Json
     * @author 段誉
     * @date 2022/9/16 18:06
     */
    public function resetPassword()
    {
        $params = (new PasswordValidate())->post()->goCheck('resetPassword');
        $result = UserLogic::resetPassword($params);
        if (true === $result) {
            return $this->success('操作成功', [], 1, 1);
        }
        return $this->fail(UserLogic::getError());
    }


    /**
     * @notes 修改密码
     * @return \think\response\Json
     * @author 段誉
     * @date 2022/9/20 19:16
     */
    public function changePassword()
    {
        $params = (new PasswordValidate())->post()->goCheck('changePassword');
        $result = UserLogic::changePassword($params, $this->userId);
        if (true === $result) {
            return $this->success('操作成功', [], 1, 1);
        }
        return $this->fail(UserLogic::getError());
    }


    /**
     * @notes 获取小程序手机号
     * @return \think\response\Json
     * @author 段誉
     * @date 2022/9/21 16:46
     */
    public function getMobileByMnp()
    {
        $params = (new UserValidate())->post()->goCheck('getMobileByMnp');
        $params['user_id'] = $this->userId;
        $result = UserLogic::getMobileByMnp($params);
        if ($result === false) {
            return $this->fail(UserLogic::getError());
        }
        (new User())->where("user_id",$params['user_id'])->update(['qualifications'=>1]);
        return $this->success('绑定成功', [], 1, 1);
    }


    /**
     * @notes 编辑用户信息
     * @return \think\response\Json
     * @author 段誉
     * @date 2022/9/21 17:01
     */
    public function setInfo()
    {
        $params = (new SetUserInfoValidate())->post()->goCheck(null, ['id' => $this->userId]);
        $result = UserLogic::setInfo($this->userId, $params);
        if (false === $result) {
            return $this->fail(UserLogic::getError());
        }
        return $this->success('操作成功', [], 1, 1);
    }

    public function qiye()
    {
        $user=  $this->request->get();
        if(!empty($user['q_id'])){
            $model= new  Enterprise();
            $list= $model->where("id","=",$user['q_id'])->findOrEmpty()->toArray();
        }else{
            $model= new  User();
            $list= $model->where("id","=",$user['user_id'])->findOrEmpty()->toArray();
        }

        return $this->success('操作成功', $list, 1, 1);
    }
    public function qiyegongying()
    {
        $user=  $this->request->get("user_id");
        $model= new  Supply();
        $list= $model->where("user_id","=",$user)->select()->toArray();
        return $this->success('操作成功', $list, 1, 1);
    }

    public function qiyecaigou()
    {
        $user=  $this->request->get();
        if(!empty($user['q_id'])){
            $model= new  Procure();
            $list= $model->where("q_id","=",$user['q_id'])->select()->toArray();
        }else{
            $model= new  Procure();
            $list= $model->where("user_id","=",$user['user_id'])->select()->toArray();
        }

        return $this->success('操作成功', $list, 1, 1);
    }

    public function vipjian()
    {
        $user=  $this->userId;
        $list=(new User())->where("id","=",$user)->findOrEmpty()->toArray();
        if( $list['vip_num']>0){
            $list['vip_num']=$list['vip_num']-1;
            $data= (new User())->where("id","=",$user)->update(["vip_num"=> $list['vip_num']]);
        }

        return $this->success('操作成功', ["data"=>1], 1, 1);
    }
    /**
     * @notes 绑定/变更 手机号
     * @return \think\response\Json
     * @author 段誉
     * @date 2022/9/21 17:29
     */
    public function bindMobile()
    {
        $params = (new UserValidate())->post()->goCheck('bindMobile');
        $params['user_id'] = $this->userId;
        $result = UserLogic::bindMobile($params);
        if($result) {
            return $this->success('绑定成功', [], 1, 1);
        }
        return $this->fail(UserLogic::getError());
    }

}